package org.apache.nutch.parse.s2jh;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.nutch.parse.HTMLMetaTags;
import org.apache.nutch.parse.Parse;
import org.apache.nutch.storage.WebPage;
import org.apache.nutch.util.TableUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.DocumentFragment;

import com.google.common.collect.Lists;

public class AlibabaHtmlParseFilter extends AbstractHtmlParseFilter{

	public static final Logger LOG = LoggerFactory.getLogger(AlibabaHtmlParseFilter.class);

	@Override
	public Parse filterInternal(String url, WebPage page, Parse parse,
			HTMLMetaTags metaTags, DocumentFragment doc) throws Exception {
		
		 List<CrawlData> crawlDatas = Lists.newArrayList();		 
	  
		 
		 String companyName= getXPathValue(doc, "//DIV[@class='company-txt']/H2[@class='company-title']/SPAN"); 
		
		 if(StringUtils.isNotBlank(companyName) /*&& StringUtils.isNotBlank(companyDetail)*/)	
		 {
			    companyName.trim();
			    byte[] b1=companyName.getBytes("gbk");
            	companyName=new String(b1,"UTF-8");
                crawlDatas.add(new CrawlData(url, "companyName", "公司名称").setTextValue(companyName, page));
               
               /* String getTouch= getXPathValue(doc, "//DIV[@class='module-content gray-b-bg']"); 
                getTouch.trim();
                byte[] b2=getTouch.getBytes("gbk");
   	    	    getTouch=new String(b2,"UTF-8");  	    	 
                crawlDatas.add(new CrawlData(url, "getTouch", "联系方式").setTextValue(getTouch, page));*/
		 }else{
			//其他页面，如首页的title
             String title = TableUtil.toString(parse.getTitle());
             title.trim();
             byte[] b3=title.getBytes("gbk");
             title=new String(b3,"UTF-8");
             crawlDatas.add(new CrawlData(url, "title", "页面名称").setTextValue(title, page));
		 }
        
         saveCrawlData(url, crawlDatas, page);

	     return parse;
	}
	
	
	@Override
	protected String getUrlFilterRegex() {
		// TODO Auto-generated method stub
		return "^http://corp.1688.com/page/index.htm?$|http://.*.1688.com/page/creditdetail.htm?$|http://detail.1688.com/offer/.*.html$|http://s.1688.com/selloffer/.*.html$|http://www.1688.com|http://fuzhuang.1688.com/.*|http://auto.1688.com/.*|http://muying.1688.com/.*|http://enjoy.1688.com/.*|http://mei.1688.com/.*|http://ee.1688.com/.*|http://3c.1688.com/.*|http://jia.1688.com/.*|http://bz.1688.com/.*|http://plas.1688.com/.*|http://light.1688.com/.*|http://fangzhi.1688.com/.*|http://ye.1688.com/";
	}

	@Override
	protected boolean isParseDataFetchLoadedInternal(String url, String html) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	protected boolean isContentMatchedForParse(String url, String html) {
		// TODO Auto-generated method stub
		return true;
	}

	

}
